import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
		config: {}, // 配置信息
		userInfo: {},// 用户基本信息
		news: false, // 收到新消息
		art: false, //是否为艺考生
		parent_id: null,
		agent_id: null,
		SNAP_STORAGE_LIST: [],
		fcData:{
			url: 'https://api.zmacp.cn/api/',
			token: '',
			code: '',
			loginAccount: 'makehanmu',
			password: 'makehanmu123'
		},
		roomList:[],//课堂列表数据
		historyList:[],//保存历史数据
		newData:[],//圈子数据
		userData:{},//用户圈子数据
		showModel: 'none'
    },

    getters: {
    	getUserInfo: (state) => {
    		return state.userInfo
    	}
    },
    mutations: {
		//推荐圈子新闻数据
		newFunction(state,data){
			state.newData=data
		},
		setShowModel(state, key) {
			state.showModel = key
		},
		//用户回答
		userFunction(state,data){
			state.userData=data
		},
		//保存历史搜索数据
		historyFunction(state,data){
			state.historyList=data
		},
		//课堂数据
		classList(state,data){
			state.roomList=data
		},
		logout: (state) => {
			state.userInfo = new Object()
		},
		// 保存用户信息
		saveUserInfo: (state, obj) => {
			Object.assign(state.userInfo,obj)
		},
		// 收到消息
		newMessage: (state) => {
			state.news = true
		},
		// 消息已读
		haveRead: (state) => {
			state.news = false
		},
		saveArtStatus: (state) => {
			state.art = true
		},
		// 保存应用配置信息
		saveConfig: (state, obj) => {
			state.config = obj
		},
		// 临时存储
		snapStorage: (state, obj) => {
			let index = null
			for (var i = 0; i < state.SNAP_STORAGE_LIST.length; i++) {
				if (state.SNAP_STORAGE_LIST[i].key == obj.key) {
					index = i
					Object.assign(state.SNAP_STORAGE_LIST[i].val, obj.val)
					break
				}
			}
			if (index === null) {
				state.SNAP_STORAGE_LIST.push({key: obj.key,val: obj.val})
			}
		},
		// 删除临时存储
		deleteStorage: (state, key) => {
			for (var i = 0; i < state.SNAP_STORAGE_LIST.length; i++) {
				if (state.SNAP_STORAGE_LIST[i].key == key) {
					state.SNAP_STORAGE_LIST.splice(i, 1)
					break 
				}
			}
		},
		// 保存上级id
		saveParentId: (state, id) => {
			state.parent_id = id
		},
		// 保存代理id
		saveAgentId: (state, id) => {
			state.agent_id = id
		},
		// 方橙测评
		saveFcData: (state,obj) => {
			state.fcData[obj.k] = obj.v
		}
    },
	actions: {
		getUserInfo: ({commit}) => {
			const data = uni.getStorageSync('userInfo')
			if(data) commit('saveUserInfo',data)
		},
		// 设置用户信息
		setUserInfo: ({commit},obj) => {
			uni.setStorage({
				key: 'userInfo',
				data: obj,
				success: (res)=>{
					commit('saveUserInfo',obj)
				}
			})
		},
		// 获取指定存储
		getSnapStorage: ({state},key) => {
			for (var i = 0; i < state.SNAP_STORAGE_LIST.length; i++) {
				if (state.SNAP_STORAGE_LIST[i].key == key) {
					return state.SNAP_STORAGE_LIST[i].val
				}
			}
		},
		// 获取指定存储
		getSnapStorages: ({state},keys) => {
			let arr = {}
			let key_arr = []
			if (typeof(keys) == 'string') {
				key_arr.push(keys)
			} else {
				key_arr = keys
			}
			for (var i = 0; i < key_arr.length; i++) {
				for (var j = 0; j < state.SNAP_STORAGE_LIST.length; j++) {
					if (state.SNAP_STORAGE_LIST[j].key == key_arr[i]) {
						arr[key_arr[i]] = state.SNAP_STORAGE_LIST[i].val
						break
					}
				}
			}
			return arr
		},
		// 艺考设置
		setTabBar: async ({commit},obj) => {
			await Promise.all([
				uni.setTabBarItem({
				  index: 0,
				  text: '首页',
				  pagePath: '/pages/art_index/index'
				  // iconPath: '/path/to/iconPath',
				  // selectedIconPath: '/path/to/selectedIconPath'
				}),
				uni.setTabBarItem({
				  index: 1,
				  text: '志愿',
				  pagePath: '/pages/art_probability/index'
				  // iconPath: '/path/to/iconPath',
				  // selectedIconPath: '/path/to/selectedIconPath'
				})
			])
			commit('saveArtStatus')
		}
	}
})

export default store
